using System;
using System.Collections.Generic;
using System.Text;
using DataAccessTecnoPez;
using System.Threading;
using System.Diagnostics;

namespace BussinessLogicTecnoPez
{
    /// <summary>
    /// Clase que representa, cualquier entidad administrada por el getor generico.
    /// Implementa un patron singleton mulithread para mejorar la performance.
    ///  </summary>
    /// <typeparam name="T">Tipo de entidad a administrar</typeparam>
    [Serializable]
    public abstract class EntityBase<T> where T: new()
    {
        
        
        public static GenericManager<T> getManager
        {
            get
            {
               Type t = typeof(T);
                GenericManager<T> manager;
                manager = Thread.GetData(Thread.GetNamedDataSlot(t.FullName)) as GenericManager<T>;
    

                if (manager != null)
                {
                    Debug.WriteLine(DateTime.Now.ToShortTimeString() + " hilo " + Thread.CurrentThread.GetHashCode() + " pidio " + t.FullName + " y ya lo tenia.");
                    return manager;
                }
                else
                {
                    Debug.WriteLine(DateTime.Now.ToShortTimeString() + " hilo " + Thread.CurrentThread.GetHashCode() + " pidio " + t.FullName + " instancio un nuevo objeto.");
                    manager = new GenericManager<T>();
                    Thread.SetData(Thread.GetNamedDataSlot(t.FullName), manager);
                    return manager;
                }
            }
        }
        
    }
}
